import { validationResult } from "express-validator";
import { errorResponse } from "../../utils/result.js";

export default (validator) => {
  return async (req, res, next) => {
    await Promise.all(validator.map((validation) => validation.run(req)));
    const errors = validationResult(req);
    if (!errors.isEmpty()) {
      return res.status(401).json(errorResponse(errors.array()[0].msg));
    }
    next();
  };
};
